package com.zscat.mallplus.qsg.contrller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.qsg.QsgDevice;
import com.zscat.mallplus.qsg.QsgGatewayDevice;
import com.zscat.mallplus.qsg.QsgGatewayInfo;
import com.zscat.mallplus.qsg.service.IDeviceService;
import com.zscat.mallplus.qsg.service.IGatewayDeviceService;
import com.zscat.mallplus.qsg.service.IQsgGatewayInfoService;
import com.zscat.mallplus.util.UserUtils;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "GatewayDeviceController", description = "网关设备管理")
@RequestMapping("/admin/qsg/gatewayDevice")
public class GatewayDeviceController {

//    @Resource
//    private IQsgGatewayInfoService gatewayInfoService;
    @Resource
    private IGatewayDeviceService gatewayDeviceService;
    @Resource
    private IDeviceService deviceService;

    @SysLog(MODULE = "qsg", REMARK = "根据条件查询所有网关设备列表")
    @ApiOperation("根据条件查询所有网关设备列表")
    @GetMapping(value = "/list")
    public Object getGateWayByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper qw = new QueryWrapper();
        qw.orderByDesc("create_datetime");
        qw.eq("store_id",UserUtils.getCurrentMember().getQsgStoreId());
        try {
            IPage<QsgGatewayDevice> gatewayDeviceIPage = gatewayDeviceService.page(new Page<>(pageNum, pageSize), qw);
            if (gatewayDeviceIPage.getRecords().size()>0){
                for (QsgGatewayDevice gatewayDevice : gatewayDeviceIPage.getRecords()){
                    QsgDevice device = deviceService.getById(gatewayDevice.getDeviceId());
                    if (null!=device){
                        gatewayDevice.setBelongDeviceName(device.getMachineName());
                    }
                }
            }
            return new CommonResult().success(gatewayDeviceIPage);
        } catch (Exception e) {
            log.error("根据条件查询所有网关设备列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }


    @SysLog(MODULE = "qsg", REMARK = "保存网关设备表")
    @ApiOperation("保存网关设备表")
    @PostMapping(value = "/create")
    public Object saveGateway(@RequestBody QsgGatewayDevice entity) {
        try {
            entity.setCreateDatetime(new Date());
            entity.setStoreId(UserUtils.getCurrentMember().getQsgStoreId().intValue());
            if (gatewayDeviceService.save(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("保存网关设备表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "qsg", REMARK = "更新网关设备表")
    @ApiOperation("更新网关设备表")
    @PostMapping(value = "/update/{id}")
    public Object updateGateway(@RequestBody QsgGatewayDevice entity) {
        try {
            if (gatewayDeviceService.updateById(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("更新网关设备表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "qsg", REMARK = "删除网关设备表")
    @ApiOperation("删除网关设备表")
    @GetMapping(value = "/delete/{id}")
    public Object deleteGateway(@ApiParam("网关设备表id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("网关设备表id");
            }
            if (gatewayDeviceService.removeById(id)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("删除网关设备表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "qsg", REMARK = "给网关设备表分配网关设备表")
    @ApiOperation("查询网关设备表明细")
    @GetMapping(value = "/{id}")
    public Object getGatewayById(@ApiParam("网关设备表id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("网关设备表id");
            }
            QsgGatewayDevice gatewayDevice = gatewayDeviceService.getById(id);
            return new CommonResult().success(gatewayDevice);
        } catch (Exception e) {
            log.error("查询网关设备表明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
    }
}
